<?php

namespace Trawind\Basics\Traits\Transform;


use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;

/**
 * Trait ValidationBreadcrumbField
 * @package Trawind\Basics\Traits
 */
trait ValidationBreadcrumbField
{
    /**
     * Get the attributes and values that were validated.
     *
     * @return array
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    public function validated()
    {
        if ($this->validator->invalid()) {
            throw new ValidationException($this->validator);
        }

        $results = [];

        $missingValue = Str::random(10);
        foreach (array_keys($this->validator->getRules()) as $key) {
            $value = data_get($this->validator->getData(), $key, $missingValue);

            if ($value !== $missingValue && (!is_array($value) || !$value)) {
                Arr::set($results, $key, $value);
            }
        }

        return $results;
    }
}